﻿using System;
using System.Configuration;
using System.Linq;
using System.Xml.Linq;
using System.Data.Linq;
using BusinessLogic.Model;
using BusinessLogic.Model.Repository.NewsItemRepositories;
using BusinessLogic.Model.Repository;
using BusinessLogic;

public partial class RSS : BasePage
{
    #region PageEvents

    protected void Page_Load(object sender, EventArgs e)
    {
        //get the protocol prefix, the host and eventual port from the url.
        //"www." sux! If we leave it we risk duplicate feeds because the GUID and the item URL will differ.
        string authority = Request.Url.Authority.StartsWith("www.") ? Request.Url.Authority.Substring(4) : Request.Url.Authority;
        string host = Request.Url.Scheme + Uri.SchemeDelimiter + authority;
        string articleUrl = host + ResolveUrl("~/ViewNewsItem.aspx") + "?" + QueryStringConstants.NewsItemID + "=";

        string tagParam = Request.QueryString[QueryStringConstants.Tag];

        INewsItemRepository newsItemRepository = RepositoryFactory.Instance.GetNewsItemRepository();

        var query = newsItemRepository.GetByTags(tagParam).Take(15).ToList();

        //build XML out of the database query
        var xmlQuery = from ni in query
                       select new XElement("item",
                         new XElement("title", Server.HtmlDecode(ni.Title)), //this is not HTML so no HTML encoding is needed
                         new XElement("description", ni.ShortText), //this part seems to be HTML so it has better stay encoded
                         new XElement("link", articleUrl + ni.NewsItemID),
                         new XElement("guid", articleUrl + ni.NewsItemID),
                         new XElement("comments", articleUrl + ni.NewsItemID),
                         new XElement("pubDate", ni.PostDate.ToUniversalTime().ToString("R")));


        //get the last time the content has changed
        DateTime lastBuildDate = (from ni in query
                                  select ni).Max(ni => ni.LastEditedDate == null || ni.PostDate > ni.LastEditedDate ?
                                      ni.PostDate : (DateTime)ni.LastEditedDate);

        XDocument document = new XDocument(
            new XDeclaration("1.0", "utf-8", null),
            new XElement("rss",
                new XElement("channel",
                    new XElement("title", "The No-Blog Articles"),
                    new XElement("link", host),
                    new XElement("description", "This is not a blog. This is a no-blog."),
                    new XElement("lastBuildDate", lastBuildDate.ToUniversalTime().ToString("R")),
                    xmlQuery),//end of channel element
                    new XAttribute("version", "2.0"))//end of rss element
                );

        Response.ContentType = "application/rss+xml";
        document.Save(Response.Output);
        //Response.End(); this would break OutputCache
    }

    #endregion
}
